package com.danchuang.consumer.service;

import com.danchuang.consumer.dto.Order;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @author haitao
 * @date 2021/8/19 5:13 下午
 */
@Service
@Slf4j
public class ConsumerQosTest {

    /**
     * 这里测试多个消费者超过SimpleListenerMessageContainer
     *
     * */
    @RabbitListener(queues = {"test_direct_queue"})
    public void onDirectMessage1(@Payload Order order, Channel channel, @Headers Map<String, Object> headers){

        System.out.println("消费者1消费消息：" + order);
        //获取消息标签
        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手动ack，不批量
        try {
            channel.basicAck(deliverTag, false);
        } catch (IOException e) {
            log.error("消费者1ACK失败");
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = {"test_direct_queue"})
    public void onDirectMessage2(@Payload Order order, Channel channel, @Headers Map<String, Object> headers){

        System.out.println("消费者1消费消息：" + order);
        //获取消息标签
        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手动ack，不批量
        try {
            channel.basicAck(deliverTag, false);
        } catch (IOException e) {
            log.error("消费者2ACK失败");
            e.printStackTrace();
        }
    }

    /**
     * 这里测试多个消费者超过SimpleListenerMessageContainer
     *
     * */
    @RabbitListener(queues = {"test_direct_queue"})
    public void onDirectMessage3(@Payload Order order, Channel channel, @Headers Map<String, Object> headers){

        System.out.println("消费者3消费消息：" + order);
        //获取消息标签
        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手动ack，不批量
        try {
            channel.basicAck(deliverTag, false);
        } catch (IOException e) {
            log.error("消费者3ACK失败");
            e.printStackTrace();
        }
    }

    /**
     * 这里测试多个消费者超过SimpleListenerMessageContainer
     *
     * */
    @RabbitListener(queues = {"test_direct_queue"})
    public void onDirectMessage4(@Payload Order order, Channel channel, @Headers Map<String, Object> headers){

        System.out.println("消费者4消费消息：" + order);
        //获取消息标签
        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手动ack，不批量
        try {
            channel.basicAck(deliverTag, false);
        } catch (IOException e) {
            log.error("消费者4ACK失败");
            e.printStackTrace();
        }
    }

    /**
     * 这里测试多个消费者超过SimpleListenerMessageContainer
     *
     * */
    @RabbitListener(queues = {"test_direct_queue"})
    public void onDirectMessage5(@Payload Order order, Channel channel, @Headers Map<String, Object> headers){

        System.out.println("消费者5消费消息：" + order);
        //获取消息标签
        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //手动ack，不批量
        try {
            channel.basicAck(deliverTag, false);
        } catch (IOException e) {
            log.error("消费者5ACK失败");
            e.printStackTrace();
        }
    }


}
